package com.example.sunmiv1.http

import android.content.Intent.getIntent
import android.text.TextUtils
import android.util.Log
import com.example.sunmiv1.entity.GetUserLabelInfo
import com.example.sunmiv1.utils.GsonTools
import com.example.sunmiv1.utils.Tools
import com.example.sunmiv1.utils.net.Common
import com.example.sunmiv1.utils.net.GsonUtils
import com.example.sunmiv1.utils.net.HttpUtils
import com.example.sunmiv1.utils.net.TextCallBack
import org.application.ClientApplication.context
import org.json.JSONException
import org.json.JSONObject


object HttpManager {
    private val TAG=javaClass.simpleName
    fun getUserVipLabel(vipId:String,httpListener: HttpListener<String>) {
        var jsonObject = JSONObject()
        try {
            jsonObject.put("vid",vipId)
            jsonObject = Tools.createParameterCode(jsonObject)
            Log.e(TAG, "getUserLabel: vipId=$vipId", )
            HttpUtils.post(
                context,
                Common.GET_USER_LABEL,
                jsonObject,
                object : TextCallBack<Any?>() {
                    override fun onSuccess(text: String) {
                        Log.e(TAG, "onSuccess: text=$text", )
                        val getUserLabelInfo = GsonUtils.JsonToBean<GetUserLabelInfo>(
                            text,
                            GetUserLabelInfo::class.java
                        )
                        Log.e(TAG, "onSuccess: json=${GsonTools.beanToJson(getUserLabelInfo)}", )
                        if (getUserLabelInfo.status ==1) {
                            var labels = ""
                            for (i in 0 until getUserLabelInfo.getData().getList().size) {
                                labels = (labels + getUserLabelInfo.getData().getList().get(i)
                                    .getName()).toString() + ","
                            }
                            Log.e(TAG, "onSuccess: labels=$labels", )
                            if(labels.isNotBlank()){
                                httpListener.onSucess(labels)
                                return
                            }
                        }
                        httpListener.onSucess("")
                    }

                    override fun onFailure(e: ResponseException) {
                        Log.e(TAG, "onFailure: e=$e" )
                        httpListener.onError(e.message?:"")
                    }
                })
        } catch (e: JSONException) {
            e.printStackTrace()
            Log.e(TAG, "getUserLabel: error=$e" )
            httpListener.onError(e.message?:"")
        }
    }
}